package org.jypj.dev.util;

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.InputFormatException;

import java.io.File;

/**
 * Created by chenyuyuyu on 2017/5/11.
 */
public class Mp3Util {

    public static void main(String[] args) throws Exception {
        String path1 = "D:/20170517221935227.mp3";
        String path2 = "D:/bb/asdasd.mp3";
        int i = path2.lastIndexOf("/");
        String substring = path2.substring(i + 1);
        System.out.println(substring);
        //changeToMp3(path1, path2);
    }

    public static void changeToMp3(String sourcePath, String targetPath) {
        File source = new File(sourcePath);
        File target = new File(targetPath);
        AudioAttributes audio = new AudioAttributes();
        Encoder encoder = new Encoder();

        audio.setCodec("libmp3lame");
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp3");
        attrs.setAudioAttributes(audio);

        try {
            encoder.encode(source, target, attrs);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InputFormatException e) {
            e.printStackTrace();
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }
}
